package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.dao.BaseAssetOperationTrajectoryMapper;
import com.redoor.app.stake.basicdata.model.BaseAssetOperationTrajectory;
import com.redoor.app.stake.basicdata.model.BussinessTypeEnum;
import com.redoor.app.stake.basicdata.model.LogEnum;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseAssetOperationTrajectoryCriteria;
import com.redoor.app.stake.basicdata.service.BaseAssetOperationDetailLogService;
import com.redoor.app.stake.basicdata.service.BaseAssetOperationTrajectoryService;

import com.redoor.app.stake.basicdata.service.LogService;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by wangjunlei on 2018-03-03 19:26:43.
 */
@Service
public class BaseAssetOperationTrajectoryServiceImpl extends AbstractService<BaseAssetOperationTrajectory, String> implements BaseAssetOperationTrajectoryService {

    @Autowired
    private BaseAssetOperationTrajectoryMapper baseAssetOperationTrajectoryMapper;

    @Autowired
    private LogService logService;

    @Autowired
    private BaseAssetOperationDetailLogService baseAssetOperationDetailLogService;

    @Autowired
    private BaseUserService userService;

    @Override
    public int saveAll(List<BaseAssetOperationTrajectory> operationTrajectories) {
        if(operationTrajectories != null && operationTrajectories.size() > 0){
            // baseHsfOperationLogMapper.insertList() 只能用自增的方式新增，当前表结构无法使用
            baseAssetOperationTrajectoryMapper.batchInsert(operationTrajectories);
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public void addTraceLog(String uuid, String tag,BussinessTypeEnum.TRACE_BUSINESS_TYPE assettype, BussinessTypeEnum.TRACE_TYPE sourcetype, String sourceuuid, Object oldStation, Object newStation) {
        BaseAssetOperationTrajectory bean  = new BaseAssetOperationTrajectory();
        bean.setActiontype(tag);
        bean.setAssettype(assettype.getType());
        bean.setAssetuuid(uuid);
        bean.setOperatordate(new Date());
        if(sourcetype == BussinessTypeEnum.TRACE_TYPE.WEB && null != userService.getShiroUser(null)){
            bean.setSourceuuid(userService.getShiroUser(null).getUuid());
            bean.setSourcename(userService.getShiroUser(null).getLoginname());
        } else {
            bean.setSourceuuid(sourceuuid);
        }
        bean.setSourcetype(sourcetype.getType());

        bean.setUuid(UUIDTool.getUUID());
        logService.addLogToRedis(bean, LogEnum.ASSET_TRACE_LOG);

        baseAssetOperationDetailLogService.addTraceDetailLogToRedis(oldStation,newStation,bean);
    }

    @Override
    public List<BaseAssetOperationTrajectory> getTraceList(BussinessTypeEnum.TRACE_BUSINESS_TYPE bussinessTypeEnum, String uuid) {
        BaseAssetOperationTrajectoryCriteria param = new BaseAssetOperationTrajectoryCriteria();
        param.setAssetType(bussinessTypeEnum.getType());
        param.setAssetUuid(uuid);
        return baseAssetOperationTrajectoryMapper.getTraceList(param);
    }
}
